package com.java.ssm.controerll;

import com.java.ssm.entity.Location;
import com.java.ssm.entity.Orders;
import com.java.ssm.entity.Staff;
import com.java.ssm.entity.Warehousing;
import com.java.ssm.service.impl.LocationServiceImpl;
import com.java.ssm.service.impl.OrderServiceImpl;
import com.java.ssm.service.impl.StaffServiceImpl;
import com.java.ssm.service.impl.WarehousingServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static org.apache.ibatis.ognl.DynamicSubscript.all;

/**
 * (Warehousing)表控制层
 *
 * @author makejava
 * @since 2021-01-18 10:58:12
 */
@Controller
@RequestMapping("warehousing/")
public class WarehousingController {
    /**
     * 服务对象
     */
    @Resource
    private WarehousingServiceImpl warehousingService;
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private StaffServiceImpl staffService;
    @Resource
    private LocationServiceImpl locationService;

    /**
     * 查询入库订单的全部数据
     */
    @RequestMapping("Warehousing_All")
    public String Warehousing_All(HttpSession session) {
        //查询入库订单的全部数据
        List<Warehousing> warehousing = warehousingService.getAllRu();
        session.setAttribute("warehousingAll",warehousing);
        //查询货位的全部数据
        List<Location> locations = locationService.queryAll(new Location());
        session.setAttribute("locationAll",locations);

        return "jsp/warehousing/stock-order";
    }

    /**
     * 查询出库订单的全部
     */
    @RequestMapping("/getAll")
    public String getAll(HttpServletRequest request){
            List<Warehousing> all = warehousingService.getAll();
            request.getSession().setAttribute("getallwar",all);
        return "jsp/delivery/out-record";
    }

    /**
     * 添加入库订单
     */
    @RequestMapping("Warehousing_Add")
    @ResponseBody
    public String Warehousing_Add(HttpServletResponse response, HttpSession session, String number, String pass) throws IOException {
        System.out.println("订单"+number);
        System.out.println("货位号"+pass);
        //这条订单是否已入库
        List<Warehousing> warehousings = warehousingService.getAllRu();
        System.out.println(warehousings);

        //查询前台输入的订单是否存在
        Orders orders = orderService.queryByoNumber(number);
        System.out.println("订单表的数据"+orders);
        Integer admin = (Integer) session.getAttribute("admin");
        System.out.println("数字"+admin);

        //查询操作员
        Staff staff = staffService.queryById(admin);
        System.out.println("操作人"+staff);

        //查询货位号
        List<Location> locations = locationService.queryAll(new Location());

            //判断订单是否存在
            if(orders!=null){
                //循环判断订单表
                for (Warehousing warehousing1 : warehousings) {
                    System.out.println("这是入库订单表的全部数据"+warehousing1);
                    //判断订单是否已入库，如果存在就提示，不存在则添加
                    if (number.equals(warehousing1.getoId().getoNumber())){

                        System.out.println("该订单已入库");
                        return "该订单已入库";
//                    return "jsp/warehousing/admin-add";
                    }else{
                        //查看前台输入的货位是否存在，存在则能添加
                        for (Location location : locations) {
                            if(pass.equals(location.getLNumber())){
                                Staff staff1 = new Staff();
                                Warehousing warehousing = new Warehousing(orders,"",1,new Date(),1,staff1);
                                warehousingService.insert(warehousing);
                                return "入库成功，待审核";
                            }else{
                                return "货位不存在，请重新输入";
                            }
                        }
                    }
                }
            }else{
                System.out.println("您输入的订单不存在");
                return "您输入的订单不存在";
            }
        return "no";
    }

    /**
     * 软删除
     */
    @RequestMapping("Warehousing_RUp")
    public String Warehousing_RUp(Integer id){
        System.out.println("软删除的id"+id);
        Warehousing warehousing = warehousingService.queryById(id);
        System.out.println(warehousing);
        warehousingService.getRUp(id);
        return "redirect:/warehousing/Warehousing_All";
    }

    /**
     * 软删除
     */
    @RequestMapping("Warehousing_sh")
//    @ResponseBody
    public String Warehousing_sh(String str){
        String[] split = str.split(",");
        for (String s : split) {
            Warehousing warehousing = new Warehousing();
            warehousing.setwId(Integer.parseInt(s));
            warehousing.setwState(2);
            warehousingService.update(warehousing);
        }
        return "redirect:/warehousing/Warehousing_All";
    }

}